package com.hotel.reservation.controller.admin;

import com.hotel.reservation.po.NoticePo;
import com.hotel.reservation.service.NoticeService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebParam;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @version 0.1
 * @Author: oliver chen
 * @Description: 酒店资讯
 * @Date:Create：in 2021/8/2 10:36
 * @Modified By：
 */
@Controller
@RequestMapping("/admin/notice")
public class AdminNoticeController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/selectAll")
    public String selectAll(Integer pageCur,Model model,String flag){
        List<NoticePo> noticePos = noticeService.selectAll();
        model.addAttribute("notice",noticePos);
        if (noticePos==null){
            noticePos=new ArrayList<>();
        }
        int temp =noticePos.size();
        model.addAttribute("totalCount",temp);
        int totalPage=0;
        if (temp==0){
            totalPage=0;//总页数
        }else {
            //返回大于或等于指定表达式的最小整数
            totalPage=(int)Math.ceil((double) temp/10);
        }
        if (pageCur==null){
            pageCur=1;
        }
        if ((pageCur-1)*10>temp){
            pageCur=pageCur-1;
        }
        //分页查询
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("startIndex",(pageCur-1)*10);//起始位置
        map.put("perPageSize",10);//每页10个
        noticePos = noticeService.selectAllRoomByPage(map);
        model.addAttribute("allRooms",noticePos);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pageCur",pageCur);
        if (flag.equals("delete")){
            model.addAttribute("allNotice",noticePos);
            return "admin/deleteSelectNotice";
        }else {
            return "admin/selectNotice";
        }

    }

    @RequestMapping("selectANotice")
    public String selectANotice(Model model,int noticeId){
        NoticePo n = noticeService.selectANotice(noticeId);
        model.addAttribute("notice",n);
        return "admin/noticeDetail";
    }
    @RequestMapping("toInsertANotice")
    public String toInsertANotice(Model model,String flag){
        NoticePo n = new NoticePo();
        model.addAttribute("notice",n);
        model.addAttribute("flag",flag);
        return "admin/addNotice";

    }

    @RequestMapping("insertANotice")
    public String insertANotice(Model model,NoticePo notice){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//可以方便地修改日期格式
        String nowDate = dateFormat.format( now );
        notice.setNoticeTime(nowDate);
        Integer integer = noticeService.insertANotice(notice);
        if (integer==1){
            model.addAttribute("message","添加成功");
        }else {
            model.addAttribute("message","添加失败");
        }
        return "forward:/admin/notice/selectAll?flag=insert";
    }
    @RequestMapping("toUpdateANotice")
    public String toUpdateANotice(int noticeId,Model model,String flag){
        NoticePo noticePo = noticeService.selectANotice(noticeId);
        model.addAttribute("notice",noticePo);
        model.addAttribute("flag",flag);
        return "admin/addNotice";
    }

    @RequestMapping("updateANotice")
    public String updateANotice(NoticePo notice,Model model){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//可以方便地修改日期格式
        String nowDate = dateFormat.format( now );
        notice.setNoticeTime(nowDate);
        Integer integer = noticeService.alterANotice(notice);
        if (integer==1){
            model.addAttribute("message","修改成功");
        }else {
            model.addAttribute("message","修改失败");
        }
        return "forward:/admin/notice/selectAll?flag=update";
    }

    @RequestMapping("/deleteANotice")
    public String deleteANotice(Model model,int noticeId){
        Integer integer = noticeService.deleteANotice(noticeId);
        if (integer==1){
            model.addAttribute("message","删除成功");
        }else {
            model.addAttribute("message","删除失败");
        }
        return "forward:/admin/notice/selectAll?flag=delete";
    }

    @RequestMapping("/deleteNotice")
    public String deleteNotice(Model model,int[] ids){
        List<Integer> list = new ArrayList<Integer>();
        for (int i:ids){
            list.add(i);
        }
        Integer integer = noticeService.deleteNotice(list);
        if (integer>0){
            model.addAttribute("message","删除成功");
        }else {
            model.addAttribute("message","删除失败");
        }
        return "forward:/admin/notice/selectAll?flag=delete";
    }
}
